using DapperDemo.Infrastructure;
using Microsoft.AspNetCore.Mvc;
using sdmap.ext.Dapper;
using SdmapDemo.Api.Dtos;
using System.Data;

namespace SdmapDemo.Api.Controllers;

[ApiController]
[Route("[controller]")]
public class OrdersController : ControllerBase
{
    private readonly IBaseRepository _baseRepository;

    public OrdersController(IBaseRepository baseRepository)
    {
        _baseRepository = baseRepository;
    }

    [HttpGet]
    public IEnumerable<OrderDto> GetList()
    {
        using IDbConnection con = _baseRepository.GetOpenConn();
        var orderDtos = con.QueryByMap<OrderDto>("Orders.GetOrders");
        return orderDtos;
    }

    [HttpGet("{id}")]
    public OrderDto Get(int id)
    {
        using IDbConnection con = _baseRepository.GetOpenConn();
        var orderDto = con.QueryFirstByMap<OrderDto>("Orders.GetOrderById", new
        {
            Id = id
        });
        return orderDto;
    }
}
